using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    [PersistChildren(true)]
    [ParseChildren(false)]
    [ToolboxData("<{0}:FillColumn runat=server></{0}:FillColumn>")]
    [Designer(typeof(FillColumnDesigner))]
    public class FillColumn : BaseColumn
    {
        internal override int GetColumnWidth()
        {
            return 1;
        }

        protected override string GetClass()
        {
            return "FillColumn";
        }

        protected override void AddInnerAttributesToRender(HtmlTextWriter writer)
        {
            //find containing columns
            Columns cols = null;
            Control current = this;
            while (cols == null && current.Parent != null)
            {
                cols = current.Parent as Columns;
                current = current.Parent;
            }
            Unit left = Unit.Empty;
            Unit right = Unit.Empty;
            if (cols != null)
            {
                foreach (Control ctrl in cols.Controls)
                {
                    if (ctrl is FixedWidthColumn && ctrl.Visible)
                    {
                        FixedWidthColumn col = ctrl as FixedWidthColumn;
                        if (col.Float == FixedWidthColumn.FloatOptions.Left)
                        {
                            if (left == Unit.Empty)
                            {
                                left = col.Width;
                            }
                            else if (left.Type == col.Width.Type)
                            {
                                left = new Unit(left.Value + col.Width.Value, left.Type);
                            }
                            else
                            {
                                throw new ApplicationException("Cannot mix diffent unit types when using fixed width columns");
                            }
                        }
                        else if (col.Float == FixedWidthColumn.FloatOptions.Right)
                        {
                            if (right == Unit.Empty)
                            {
                                right = col.Width;
                            }
                            else if (right.Type == col.Width.Type)
                            {
                                right = new Unit(right.Value + col.Width.Value, right.Type);
                            }
                            else
                            {
                                throw new ApplicationException("Cannot mix diffent unit types when using fixed width columns");
                            }
                        }
                    }
                }
            }

            //set margin left to be pixel width of left floating column
            if (left != Unit.Empty)
            {
                writer.AddStyleAttribute(HtmlTextWriterStyle.MarginLeft, left.ToString());
            }
            //set margin right to be right floating column
            if (right != Unit.Empty)
            {
                writer.AddStyleAttribute(HtmlTextWriterStyle.MarginRight, right.ToString());
            }
        }
    }

    public class FillColumnDesigner : BaseColumnDesigner
    {
        public override string FrameCaption
        {
            get { return "Acms Fill Columns"; }
        }
    }
}
